import { customRef } from "vue";

export function useCustomRef<T>(initialValue: T, delay: number): any {
  let timer: number;
  return customRef<T>((track, trigger) => {
    return {
      get() {
        track();
        return initialValue;
      },
      set(newValue: T) {
        clearTimeout(timer);
        initialValue = newValue;
        timer = setTimeout(() => {
          trigger();
        }, delay);
      },
    };
  });
}
